import Mock from 'mockjs';
// 生成随机名字的方法
function generateRandomName() {
    const firstNames = ['James', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer'];
    const lastNames = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia'];
    const firstName = firstNames[Math.floor(Math.random() * firstNames.length)];
    const lastName = lastNames[Math.floor(Math.random() * lastNames.length)];
    return `${firstName} ${lastName}`;
}
const dataSource = Array.from({ length: 46 }).map((_, i) => ({
    id: i,
    name: generateRandomName(),
    age: 32,
    address: `Lorem ipsum dolor sit amet consectetur adipisicing elit. Vero necessitatibus maxime veritatis sint atque fuga consequatur, facere dignissimos quaerat earum velit. Tempora eveniet aliquid ad voluptates illo magni nesciunt impedit. ${i}`,
}));

// 模拟获取表格数据的接口
Mock.mock('/api/getTableData', 'get', (options) => {
    // 解析请求体中的参数
    const body = JSON.parse(options.body);
    const pageNum = body.pageNum || 1;
    const pageSize = body.pageSize || 10;
    const name = body.name || ''; // 搜索关键字，例如：Edw

    // 实现搜索功能
    const filteredData = name
        ? dataSource.filter(item =>
            item.name.includes(name)
        )
        : dataSource;

    // 实现分页功能
    const startIndex = (pageNum - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const pagedData = filteredData.slice(startIndex, endIndex);

    return {
        code: 200,
        message: 'success',
        data: {
            list: pagedData,
            pageNum: pageNum,
            pageSize: pageSize,
            total: filteredData.length,
        },
    };
});
